using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class Dialog:Base
    {
        private DAL.Dialog dal = null;

        public Dialog()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal=new DAL.Dialog(isession);
        }

        public Common.Result Add(Model.Dialog value)
        {
            Common.Result ret = new Common.Result(false, 0, "添加失败");
            if (dal.GetModel(value.DialogCode) != null)
            {
                ret.message = "对话框编号已经存在";
                return ret;
            }
            if (!string.IsNullOrEmpty(value.ResultFieldKey) && new DAL.DialogField(isession).Get(value.DialogCode, value.ResultFieldKey) == null)
            {
                ret.message = "栏位编号不存在";
                return ret;
            }
            ret = dal.Add(value);
            return ret;
        }

        public Common.Result Update(Model.Dialog value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            var dalvalue = dal.GetModel(value.DialogCode);
            if (dalvalue == null)
            {
                ret.success = false;
                ret.count = 0;
                ret.message = "数据不存在";
                return ret;
            }
            if (!string.IsNullOrEmpty(value.ResultFieldKey) && new DAL.DialogField(isession).Get(value.DialogCode, value.ResultFieldKey) == null)
            {
                ret.message = "[栏位编号]不存在";
                return ret;
            }

            dalvalue.Title = value.Title;
            dalvalue.Database = value.Database;
            dalvalue.HQL = value.HQL;
            dalvalue.Width = value.Width;
            dalvalue.Height = value.Height;
            dalvalue.ResultFieldKey = value.ResultFieldKey;
            ret=dal.Update(dalvalue);
            return ret;
        }

        public Common.Result Delete(string ids)
        {
            var ret=dal.Delete(ids);
            return ret;
        }

        public Model.Dialog Get(string dialogCode)
        {
            return dal.GetModel(dialogCode);
        }

        public IList<Model.Dialog> Get(string where, string orderby, int start, int length, params object[] ps)
        {
            return dal.Get(where, orderby, start, length,ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }

        public System.Collections.IList DialogData(string dialogCode,string where, string orderby, int start, int length)
        {
            Model.Dialog dialog =Get(dialogCode);
            if (dialog == null) return new List<object>();
            var hql = dialog.HQL;
            if (!string.IsNullOrEmpty(where))
            {
                if (hql.IndexOf("where") == -1) hql += " where ";
                else hql += " and ";
                hql += where;
            }
            if (!string.IsNullOrEmpty(orderby)) hql += " order by " + orderby;
            var isession = DB_help.NHibernateHelper.GetCurrentSession(dialog.Database);
            IQuery query = isession.CreateQuery(hql);
            if (start > 0) query.SetFirstResult(start);
            if (length > 0) query.SetMaxResults(length);
            var ret = query.List();
            return query.List();
        }


        public int DialogDataCount(string dialogCode, string where)
        {
            Model.Dialog dialog = Get(dialogCode);
            var hql = dialog.HQL;
            if (!string.IsNullOrEmpty(where))
            {
                if (hql.IndexOf("where") == -1) hql += " where ";
                else hql += " and ";
                hql += where;
            }
            hql = hql.Trim();
            hql = hql.Substring(hql.IndexOf("from"));
            hql = "select count(*) " + hql;
            var isession = DB_help.NHibernateHelper.GetCurrentSession(dialog.Database);
            IQuery query = isession.CreateQuery(hql);
            var ret = Convert.ToInt32(query.UniqueResult());
            return ret;
        }
    }
}
